(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
  .controller('userStatusController', userStatusController);

function userStatusController ($scope, pbossOmServer,$filter,COREDATA,CoreLoading,CoreService) {
	$scope.flag = false;
	$scope.shoumun = 1;
	$scope.provinces = COREDATA.PROVINCES;

	$scope.translate = {
		provinceCodes: [],
		userStateCode: []
	};
	$scope.stateList = [
		{userStateCode:"01", userStateName:'正常'},{userStateCode:"02", userStateName:'单向停机'},
		{userStateCode:"03", userStateName:'停机'},{userStateCode:"04", userStateName:'预消号'},
		{userStateCode:"05", userStateName:'消号'},{userStateCode:"06", userStateName:'过户'},
		{userStateCode:"07", userStateName:'休眠'},{userStateCode:"08", userStateName:'待激活'},
		{userStateCode:"09", userStateName:'号码不存在'}
	];
    
	$scope.queryUserData = function(){
		var paramsData = {};
		var provinceCodes;
		provinceCodes = $scope.translate.provinceCodes.map(function(item){
          return item.provinceCode;
		});
		paramsData.provinceCodes = provinceCodes;

		$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
		if($scope.shoumun === 0){
			CoreLoading.setState(true);
			pbossOmServer.queryUserData(paramsData,queryUserData);
		}
	};

	$scope.resetValue = function(){
		$scope.translate = {
			provinceCodes: [],
			userStateCode: []
		};
	};

	var userEchart = echarts.init(document.getElementById('userState'));
	
  function queryUserData(res) {
  	CoreLoading.setState(false);
	  if(res.state === '10000' && res.data.pageData.length > 0 ){
		  $scope.flag = true;
		  $scope.userData = res.data.pageData;
		  var nameArr = [];
		  var valueRate = [];
		  angular.forEach($scope.userData,function(itemData){
			  valueRate.push((itemData.normal*100/itemData.total).toFixed(2));
			  nameArr.push(itemData.provinceName);
		  });
		  var option = {
			  title: {
				  text:'省市地区用户状态正常率',
				  x: 'center'
			  },
			  tooltip: {
				  trigger: 'axis',
				  axisPointer: {
					  type: 'shadow'
				  }
			  },
			  // calculable: true,
			  grid: {
				  x2: '13%'
			  },
			  xAxis: [
				  {
					  type: 'category',
					  data: nameArr
				  }
			  ],
			  yAxis: [
				  {
					  type: 'value',
					  min: 50,
					  max: 100,
					  axisLabel : {
						formatter: '{value} %'
					  }
				  }
			  ],
			  series: [
				  {
					  name: '正常率',
					  type: 'bar',
					  barMaxWidth: '80',
					  data: valueRate
				  }
			  ]
		  };

		  userEchart.clear();
		  userEchart.setOption(option);
		  exportExcel();

	  }else{
	  	CoreService.dialog(res.state);
	  }

  }

  function exportExcel(){
  	$scope.outData = {
  		title:'省市地区用户状态统计'
  	};
  	$scope.exportUrl = COREDATA.FORMPREFIX + 'OMdisplay/PBossOM/businessReportAnalysis/userState/export'; //表导出的请求地址
  }
	$scope.queryUserData();
}

})();